<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="手机号">
                            <a-input v-model="phone" allow-clear placeholder="请输入用户手机号" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="姓名">
                            <a-input v-model="userName" allow-clear placeholder="请输入用户姓名" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="用户ID">
                            <a-input v-model="userId" allow-clear placeholder="请输入用户ID" />
                        </a-form-item>
                    </a-col>
                    <!-- <a-col :md="8" :sm="24">
                        <a-form-item label="账户类型">
                            <a-select v-model="accountType" placeholder="请选择" @change="getData">
                                <a-select-option v-for="(item,i) in accountTypeList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col> -->
                    <a-col :md="8" :sm="24">
                    </a-col>
                    <a-col :md="4" :sm="24">
                        <a-form-item >
                            <a-button type="primary" @click="getData()">查询</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table rowKey="userId" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" size="small" :scroll="{ x: 1000, }">
            <div slot="balance" slot-scope="balance">
                <div>{{balance}}{{accountType |accountTypeFilter}}</div>
            </div>
            <div slot="freezeBalance" slot-scope="freezeBalance">
                <div>{{freezeBalance}}{{accountType |accountTypeFilter}}</div>
            </div>
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="operation(record,0)">{{accountType == '0' ? '余额充值' : '积分充值'}}</a-button>
                    <a-button type="link" @click="operation(record,1)">{{accountType == '0' ? '余额扣款' : '积分扣款'}}</a-button>
                </div>
            </template>
        </a-table>

        <a-modal
            :title="accountType == '0' ? '余额充值' : '积分充值'"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="手机号">
                        <a-input v-model="item.phone" placeholder="用户手机号" disabled allow-clear></a-input>
                    </a-form-item>
                    <a-form-item :label="accountType == '0' ? '金额' : '积分'">
                        <a-input v-model="item.gasFil" :placeholder="`请输入${accountType == '0' ? '金额' : '积分'}`" :suffix="accountType == '0' ? '元' : '积分'" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
        <a-modal
            :title="accountType == '0' ? '余额扣款' : '积分扣款'"
            :visible="subVisible"
            :confirm-loading="subConfirmLoading"
            @ok="subHandleOk"
            @cancel="subVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="手机号">
                        <a-input v-model="item.phone" placeholder="用户手机号" disabled allow-clear></a-input>
                    </a-form-item>
                    <a-form-item :label="accountType == '0' ? '金额' : '积分'">
                        <a-input v-model="item.gasFil" :placeholder="`请输入${accountType == '0' ? '金额' : '积分'}`" :suffix="accountType == '0' ? '元' : '积分'" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryUserAccount , uploadTopUpEvidence , subUserAccount } from '@/api/member';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            brandList: [],
            columns,
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            accountType: 0,
            accountTypeList: [
                { id: 0, name: '余额' },
                { id: 5, name: '积分' },
            ],
            userId: '',
            phone: '',
            userName: '',
            visible: false,
            item: {
                gasFil: 1, // GAS消耗数量
                addOrSub: 1,
                brandName: '',
            },
            confirmLoading: false,
            subVisible: false,
            subConfirmLoading: false,
        };
    },
    async created(){
        this.getData()
    },
    methods: {
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        getData(){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'phone':_this.phone,
                'userName':_this.userName,
                'userId':_this.userId,
                'accountType':_this.accountType,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryUserAccount(params)
            .then(res=>{
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 修改用户的GAS消耗数量
        operation(item,type){
            this.item = {}
            this.item.addOrSub = 1
            this.item.gasFil = ''
            this.item = Object.assign(JSON.parse(JSON.stringify(item)),this.item)
            // this.item.brandName = this.brandList.filter((brand)=>{return brand.id == this.item.brandId})[0].brandName
            if(type == 0){
                this.visible = true
            }
            if(type == 1){
                this.subVisible = true
            }
        },
        // 确认修改用户的GAS消耗
        handleOk(){
            let _this = this
            _this.confirmLoading = true
            // const params = {
            //     'tradeAmount':_this.item.gasFil,
            //     'userId': _this.item.userId,
            //     'evidenceType': '1'
            // }
            // uploadTopUpEvidence(params)
            // .then(res=>{
            //     _this.confirmLoading = false
            //     if(res.success){
            //         _this.getData()
            //         _this.visible = false
            //         _this.$notification['success']({
            //             message: '提示',
            //             description: '修改成功！',
            //             duration: 8,
            //         })
            //     }else{
            //         _this.$notification['error']({
            //             message: '提示',
            //             description: res.errMessage,
            //             duration: 8,
            //         })
            //     }
            // })
            const params = {
                'amount':_this.item.gasFil,
                'phone': _this.item.phone,
                'accountType': _this.accountType,
                'isAdd': '1'
            }
            subUserAccount(params)
            .then(res=>{
                _this.confirmLoading = false
                if(res.success){
                    _this.getData()
                    _this.visible = false
                    _this.$notification['success']({
                        message: '提示',
                        description: '加款成功！',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 账户扣款
        subHandleOk(){
            let _this = this
            _this.subConfirmLoading = true
            const params = {
                'amount':_this.item.gasFil,
                'phone': _this.item.phone,
                'accountType': _this.accountType,
            }
            subUserAccount(params)
            .then(res=>{
                _this.subConfirmLoading = false
                if(res.success){
                    _this.getData()
                    _this.subVisible = false
                    _this.$notification['success']({
                        message: '提示',
                        description: '扣款成功！',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>